﻿Public Class ventas

    Dim cadena As String = "Data Source=CESAR-PC\SQLSERVER;Initial Catalog=CAPCrepuestos;Integrated Security=True"

    Private Sub cargarCombo(ByVal combo As ComboBox, ByRef datos As DataTable, ByVal pk As String, ByVal descripcion As String)

        combo.DataSource = datos
        combo.DisplayMember = descripcion
        combo.ValueMember = pk

    End Sub

    Private Function leo_tabla(ByVal nombreTabla As String) As Data.DataTable

        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM " & nombreTabla
        tabla.Load(cmd.ExecuteReader())

        conexion.Close()

        Return tabla

    End Function

    Private Sub btn_agregarFila_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)

        grilla.Rows.Add(cmbMarca.SelectedValue, txtCodigoInterno.Text, txtDescripcion.Text, txtNroPieza.Text)

    End Sub

    Private Sub btnBuscar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBuscar.Click

        If (txtCodigoInterno.Text = "" And txtNroPieza.Text = "") Then
            txtNroPieza.Text = 0
            buscarPieza()
            MsgBox("Se busco por pieza", MsgBoxStyle.Exclamation, "ERROR")
            txtNroPieza.Text = ""
        Else
            If (txtNroPieza.Text <> "") Then buscarPieza()
            If (txtCodigoInterno.Text <> "") Then buscarCodigo()
        End If

    End Sub

    Private Sub ventas_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        cargarCombo(cmbMarca, leo_tabla("marca"), "id", "descripcion")
        limpiar()
    End Sub

    Private Sub limpiar()
        txtCodigoInterno.Text = ""
        txtDescripcion.Text = ""
        txtNroPieza.Text = ""
        txtPrecio.Text = ""
        txtCantidad.Text = ""
        cmbMarca.SelectedValue = -1

        txtCodigoInterno.Enabled = True
        txtDescripcion.Enabled = False
        txtNroPieza.Enabled = True
        txtPrecio.Enabled = False
        txtCantidad.Enabled = False
        cmbMarca.Enabled = False

        btnBuscar.Enabled = True
        btn_Agregar.Enabled = False
        btnGuardar.Enabled = False
    End Sub

    Private Sub btn_Agregar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_Agregar.Click
        If txtCantidad.Text = "" Then
            txtCantidad.Text = 1
        End If

        grilla.Rows.Add(txtNroPieza.Text, txtCodigoInterno.Text, cmbMarca.SelectedValue, txtCantidad.Text, txtPrecio.Text, txtCantidad.Text * txtPrecio.Text)
        limpiar()
        btnGuardar.Enabled = True

    End Sub
    Private Function buscarCodigo() As Integer
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable
        Dim entero As Integer = 0

        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM repuestos WHERE codigo_interno LIKE '" & txtCodigoInterno.Text & "'"

        tabla.Load(cmd.ExecuteReader())
        If (tabla.Rows.Count = 1) Then
            entero = 1
            Me.txtNroPieza.Text = tabla.Rows(0)("nro_pieza")
            Me.txtDescripcion.Text = tabla.Rows(0)("descripcion")
            Me.cmbMarca.SelectedValue = tabla.Rows(0)("marca")
            Me.txtPrecio.Text = tabla.Rows(0)("precio_final")
        Else
            MsgBox("No se encontro mannnnn! entendes?", MsgBoxStyle.Critical, "SORY ERROR")
        End If



        conexion.Close()


        Return entero

    End Function
    Private Function buscarPieza() As Integer
        Dim conexion As New Data.SqlClient.SqlConnection
        Dim cmd As New Data.SqlClient.SqlCommand
        Dim tabla As New Data.DataTable
        Dim entero As Integer = 0



        conexion.ConnectionString = cadena
        conexion.Open()
        cmd.Connection = conexion
        cmd.CommandType = CommandType.Text
        cmd.CommandText = "SELECT * FROM repuestos WHERE nro_pieza = " & txtNroPieza.Text

        tabla.Load(cmd.ExecuteReader())
        If (tabla.Rows.Count = 1) Then
            entero = 1
            Me.txtCodigoInterno.Text = tabla.Rows(0)("codigo_interno")
            Me.txtDescripcion.Text = tabla.Rows(0)("descripcion")
            Me.cmbMarca.SelectedValue = tabla.Rows(0)("marca")
            Me.txtPrecio.Text = tabla.Rows(0)("precio_final")

            btn_Agregar.Enabled = True
            btnBuscar.Enabled = False
            txtCantidad.Enabled = True

        Else
            MsgBox("No se encontro wachinnn", MsgBoxStyle.Critical, "FATAL ERRIOR")
        End If
        conexion.Close()



        Return entero

    End Function

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        Dim conexion As New SqlClient.SqlConnection
        Dim cmd As New SqlClient.SqlCommand
        Dim trans As SqlClient.SqlTransaction
        Dim index As Integer
        Dim sql As String = ""
        Dim tabla As New Data.DataTable
        conexion.ConnectionString = cadena
        cmd.CommandType = CommandType.Text
        Dim c As Integer

        For c = 0 To grilla.Rows.Count - 2

            Dim id_factura As Integer = 2
            Dim id_detalle As Integer = 2
            Dim nroPieza As Integer = System.Convert.ToInt32(grilla.Rows(index).Cells(0).Value())
            Dim cantidad As Integer = System.Convert.ToInt32(grilla.Rows(index).Cells(3).Value())
            Dim precio As Double = System.Convert.ToDouble(grilla.Rows(index).Cells(4).Value())

            sql &= "INSERT INTO detalle_factura(id_detalle, id_factura, id_repuesto, cantidad, subtotal) VALUES "
            sql &= "(" & id_detalle & ", " & id_factura & ", '" & nroPieza & "', " & cantidad & ", " & precio
            sql &= ")" & vbCrLf

        Next

        cmd.Connection = conexion
        conexion.Open()
        cmd.CommandText = sql
        trans = conexion.BeginTransaction()
        cmd.Transaction = trans

        If (cmd.ExecuteNonQuery() >= 1) Then
            cmd.Transaction.Commit()
        Else
            cmd.Transaction.Rollback()
        End If

        conexion.Close()

    End Sub

    Private Sub txtNroPieza_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtNroPieza.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtNroPieza.MaxLength = 6
    End Sub

    
    Private Sub txtCodigoInterno_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCodigoInterno.KeyPress
        If Not Char.IsLetterOrDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtCodigoInterno.MaxLength = 10
    End Sub

    Private Sub txtDescripcion_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDescripcion.KeyPress
        If Not Char.IsLetter(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtDescripcion.MaxLength = 100
    End Sub

    Private Sub txtPrecio_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtPrecio.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack And Asc(e.KeyChar) <> 46 Then e.Handled = True
        txtPrecio.MaxLength = 20
    End Sub

    Private Sub txtCantidad_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCantidad.KeyPress
        If Not Char.IsDigit(e.KeyChar) And e.KeyChar <> vbBack Then e.Handled = True
        txtNroPieza.MaxLength = 3
    End Sub


End Class